<?php
namespace frontend\controllers;

use common\models\Wxuser;
use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;

/**
 * Wx controller
 */
class WxController extends Controller
{
    public $enableCsrfValidation = false;
    /**
     * @inheritdoc
     */
//    public function behaviors()
//    {
//        return [
////            'access' => [
////                'class' => AccessControl::className(),
////                'only' => ['logout', 'signup','checktoken'],
////                'rules' => [
////                    [
////                        'actions' => ['signup'],
////                        'allow' => true,
////                        'roles' => ['?'],
////                    ],
////                    [
////                        'actions' => ['logout'],
////                        'allow' => true,
////                        'roles' => ['@'],
////                    ],
////                ],
////            ],
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'logout' => ['post'],
//                ],
//            ],
//        ];
//    }

    /**
     * @inheritdoc
     */
//    public function actions()
//    {
//        return [
//            'error' => [
//                'class' => 'yii\web\ErrorAction',
//            ],
//            'captcha' => [
//                'class' => 'yii\captcha\CaptchaAction',
//                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
//            ],
//        ];
//    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        /** @var \callmez\wechat\sdk\Wechat $wechat $wechat */
        $wechat = Yii::$app->wechat;
        $wechat->token;
        return $this->render('index');
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionChecktoken()
    {
        $wechatApi = new \vendor\wxmessage\WechatCallbackApi();
        $wechatApi->valid();
    }

    public function actionReq()
    {
        $wechatApi = new \vendor\wxmessage\WechatCallbackApi();
        $wechatApi->responseMsg();
    }


    /**
     * 路由过度跳转
     */
    public function actionGoto()
    {
        $qcode = isset($_GET['qcode'])?$_GET['qcode']:'';
        $goods_id = isset($_GET['goods_id'])?$_GET['goods_id']:'';

        /** @var \callmez\wechat\sdk\Wechat $wechat $wechat */
        $wechat = Yii::$app->wechat;
        $url = $wechat->getOauth2AuthorizeUrl(Yii::$app->params['domurl'].'wx/frontend/web/index.php?r=site/site/goodstemp&userid='.$qcode.'&goods_id='.$goods_id,'authorize','snsapi_userinfo');
        $this->redirect($url);
        return;
    }

    /**
     * @return mixed
     */
    public function actionCreateerw()
    {
        $userinfo = $this->checkAccessToken();
        if(is_object($userinfo)) {
            if ($userinfo) {
                $postData = $this->post['data'];
                $qcode = $userinfo->qcode;
                $goods_id = $postData['goods_id'];

                $url = 'http://localhost/wx/frontend/web/index.php?r=wx/goto&qcode='.$qcode.'&goods_id='.$goods_id;

                $QR = \Yii::$app->basePath.'/web/images/123.png';
                $logo  = 'http://wx.qlogo.cn/mmopen/sDuN1OzBlWWYSQw9FYxZUwOmdciaogUoN3mQOjfdkfiauUUuyvFw3GTr0O5ficBzWyC0rxgpkMqueaJhjV0aXtBMic3hDN1DRVsG/0';
                QrCode::png($url,$QR);

                if ($logo !== FALSE) {
                    $QR = imagecreatefromstring(file_get_contents($QR));
                    $logo = imagecreatefromstring(file_get_contents($logo));
                    $QR_width = imagesx($QR);//二维码图片宽度
                    $QR_height = imagesy($QR);//二维码图片高度
                    $logo_width = imagesx($logo);//logo图片宽度
                    $logo_height = imagesy($logo);//logo图片高度
                    $logo_qr_width = $QR_width / 5;
                    $scale = $logo_width/$logo_qr_width;
                    $logo_qr_height = $logo_height/$scale;
                    $from_width = ($QR_width - $logo_qr_width) / 2;
                    //重新组合图片并调整大小
                    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
                        $logo_qr_height, $logo_width, $logo_height);
                }
//输出图片
                imagepng($QR,\Yii::$app->basePath.'/web/images/12355.png');
            }
        } else {
            return $userinfo;
        }
    }
}
